package com.hsteach.web.controller.calendar.sdk;

import com.hsteach.common.annotation.Anonymous;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.dao.domain.calendar.Calendar;
import com.hsteach.dao.domain.calendar.CalendarPlan;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.framework.feign.client.FeignConfig;
import com.hsteach.service.calendar.CalendarPlanService;
import com.hsteach.service.calendar.CalendarService;
import com.hsteach.web.controller.calendar.dto.UpdateCalendarDto;
import com.hsteach.web.controller.calendar.sdk.dto.ListCalendarDetailDto;
import com.hsteach.web.controller.calendar.sdk.dto.ListCalendarDto;
import com.hsteach.web.controller.calendar.sdk.dto.ListCalendarPlanDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 周行事历主Controller
 * 
 * @author fanxquer
 * @date 2023-11-28
 */
@Api(value = "周行事历外部接口", tags = "周行事历外部接口")
@RestController
@RequestMapping("/sdk/calendar")
@Slf4j
public class CalendarSdkController extends BaseController
{
    @Autowired
    private CalendarService calendarService;

    @Autowired
    private CalendarPlanService calendarPlanService;

    /**
     * 查询周行事历主列表
     */
    @ApiOperation("查询周行事历")
    @Anonymous
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ListCalendarDto dto)
    {
        Calendar query = new Calendar();
        query.setDelStatus(BooleanEnum.TRUE.getCode());
        query.setOrgCode(dto.getOrgCode());
        List<Calendar> list = calendarService.selectCalendarList(query);
        List<UpdateCalendarDto> ret = list.stream().map(e -> {
            UpdateCalendarDto vo = new UpdateCalendarDto();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
        return getDataTable(ret);
    }

    /**
     * 获取周行事历主详细信息
     */
    @ApiOperation("查询周行事历日历明细-H5")
    @Anonymous
    @PostMapping(value = "/detail")
    public AjaxResult detail(HttpServletRequest request, @RequestBody ListCalendarDetailDto dto)
    {
        String token = request.getHeader("token");
        String orgCode = request.getHeader("orgCode");
        log.info("CalendarSdkController-detail-token-{}, orgCode-{}", token, orgCode);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("orgCode", orgCode);
        FeignConfig.contact.set(map);
        return success(calendarService.getMonthCalendar(dto.getCalendarId()));
    }

    /**
     * 查询周行事历周工作计划列表
     */
    @ApiOperation("查询周工作计划列表")
    @Anonymous
    @PostMapping("/plan/week/list")
    public TableDataInfo listAllWeek(@RequestBody ListCalendarPlanDto dto)
    {
        CalendarPlan query = new CalendarPlan();
        query.setCalendarId(dto.getCalendarId());
        query.setPlanType(1);
        query.setDelStatus(BooleanEnum.TRUE.getCode());
        query.setOrgCode(dto.getOrgCode());
        List<CalendarPlan> list = calendarPlanService.selectCalendarPlanList(query);
        return getDataTable(list);
    }

    /**
     * 查询周行事历月工作计划列表
     */
    @ApiOperation("查询月工作计划列表")
    @Anonymous
    @PostMapping("/plan/month/list")
    public TableDataInfo listAllMonth(@RequestBody ListCalendarPlanDto dto)
    {
        CalendarPlan query = new CalendarPlan();
        query.setCalendarId(dto.getCalendarId());
        query.setPlanType(2);
        query.setDelStatus(BooleanEnum.TRUE.getCode());
        query.setOrgCode(dto.getOrgCode());
        List<CalendarPlan> list = calendarPlanService.selectCalendarPlanList(query);
        return getDataTable(list);
    }
}
